package com.dcy.znoa.service.conference.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.conference.model.conferencePerson.SelectConferencePersonModel;
import com.dcy.znoa.dao.conference.ConferenceDao;
import com.dcy.znoa.dao.conference.ConferencePersonDao;
import com.dcy.znoa.domain.conference.Conference;
import com.dcy.znoa.domain.conference.ConferencePerson;
import com.dcy.znoa.domain.conference.ConferencePersonVo;
import com.dcy.znoa.service.conference.ConferencePersonService;

@Service
public class ConferencePersonServiceImpl implements ConferencePersonService {
	@Autowired
	ConferencePersonDao conferencePersonDao;
	
	@Autowired
	ConferenceDao conferenceDao;
	
	@Override
	public JsonCommon insert(ConferencePerson conferencePerson) {
		Integer result = conferencePersonDao.insert(conferencePerson);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}

	@Override
	public JsonCommon updateById(ConferencePerson conferencePerson) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JsonForList selectList(SelectConferencePersonModel selectConferencePersonModel) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JsonCommon updateByConfId(ConferencePerson conferencePerson) {
		//会议撤销时，通过会议ID更新所有参会人员ID
		EntityWrapper<ConferencePerson> ew = new EntityWrapper<>();
        ew.eq("conf_id",conferencePerson.getConfId());
        
		conferencePersonDao.update(conferencePerson, ew);
		return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}

	@Override
	public JsonForList getConferencePersonByConfId(Long confId) {
		List<ConferencePersonVo> conferencePersonVoList; 
		conferencePersonVoList = conferencePersonDao.selectConferencePersonByConfId(confId);
		
		if (conferencePersonVoList.isEmpty()) {
    		return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}else {
			return new JsonForList(conferencePersonVoList, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
	}

	@Override
	public JsonCommon updateReply(ConferencePerson conferencePerson) {
		//通过会议ID、参会人员OperID，可以确定一个ConferencePerson
		EntityWrapper<ConferencePerson> ew = new EntityWrapper<>();
        ew.eq("conf_id",conferencePerson.getConfId());  //会议ID
        ew.eq("oper_id",conferencePerson.getOperId());  //参会人员ID
        
		conferencePersonDao.update(conferencePerson, ew);
		return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}

	@Override
	public JsonCommon updateCheck(ConferencePerson conferencePerson, String qrCode) {
		List<Conference> list;       	        
        EntityWrapper<Conference> ew = new EntityWrapper<>();
        ew.eq("qr_code", qrCode);
        list = conferenceDao.selectList(ew);
        
        Conference conference = null;
        if (!list.isEmpty()) {
        	conference = list.get(0);
	    }else {
	    	return new JsonCommon(JsonCommon.SESSION_NOT_FOUND_CODE, "此会议二维码无效！");
	    }		
		
		//通过会议ID、参会人员OperID，可以确定一个ConferencePerson
		EntityWrapper<ConferencePerson> ew1 = new EntityWrapper<>();
		ew1.eq("conf_id",conference.getConfId());  //会议ID
        ew1.eq("oper_id",conferencePerson.getOperId());  //参会人员ID
        
		conferencePersonDao.update(conferencePerson, ew1);
		return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}



}
